<template>
  <view class="my-settle-container">
    <!-- 全选 -->
    <label class="radio" @click="changeAllState">
      <radio color="#C00000" :checked="isFullCheck" /><text>全选</text>
    </label>
    <!-- 合计 -->
    <view class="amount-box">
      合计:<text class="amount">${{checkedGoodsAmount}}</text>
    </view>
    <!-- 结算 -->
    <view class="btn-sellte" @click="settIement">
      结算({{checkedCount}})

    </view>

  </view>
</template>

<script>
  import {
    mapGetters,
    mapMutations,
    mapState
  } from 'vuex'
  export default {
    name: "my_settle",
    data() {
      return {
        seconds: 3,
        timer: null
      };
    },

    computed: {
      ...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
      ...mapGetters('m_user', ['addsrt']),
      ...mapState('m_user', ['token']),
      ...mapState('m_cart', ['cart']),
      isFullCheck() {
        return this.total === this.checkedCount
      },

    },

    methods: {
      ...mapMutations('m_cart', ['updateAllGoodsState', 'getGoods']),
      ...mapMutations('m_user', ['updataRedirectInfo']),
      changeAllState() {
        this.updateAllGoodsState(!this.isFullCheck)
      },
      settIement() {
        // console.log(token)
        if (!this.checkedCount) return uni.$showMsg('请选择商品')
        if (!this.addsrt) return uni.$showMsg('请选地址')
        if (!this.token) return this.delayNavigate()

        this.payOrder()
      },
      showTips(n) {
        uni.showToast({
          icon: 'none',
          title: '请登录后再结算！' + n + '秒之后自动调转到登录页',
          mask: true,
          duration: 1500
        })
      },
      delayNavigate() {
        this.showTips(this.seconds)
        this.timer = setInterval(() => {
          this.seconds--
          // this.updataRedirectInfo()
          if (this.seconds <= 0) {
            clearInterval(this.timer)
            uni.switchTab({
              url: '/pages/my/my',
              success: () => {
                this.updataRedirectInfo({
                  opentype: "switchTab",
                  form: '/pages/cart/cart'
                })
              }
            })
            return
          }
          this.showTips(this.seconds)
        }, 1000)
      },
      async payOrder() {

        const orderInfo = {
          // 开发期间，注释掉真实的订单价格，
          // order_price: this.checkedGoodsAmount,
          // 写死订单总价为 1 分钱
          order_price: 0.01,
          consignee_addr: this.addstr,
          goods: this.cart.filter(x => x.goods_state).map(x => ({
            goods_id: x.goods_id,
            goods_number: x.goods_count,
            goods_price: x.goods_price
          }))
        }
        console.log(orderInfo)
        const {
          data: res
        } = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
        if (res.mete.status !== 200) return uni.$showMsg("创建订单失败")
        const orderNumber = res.message.order_number
        console.log(orderNumber)
      }
    }
  }
</script>

<style lang="scss">
  .my-settle-container {
    position: fixed;
    bottom: 0px;
    left: 0px;
    height: 50px;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: white;
    font-size: 14px;
    padding-left: 5px;

  }

  .radio {
    display: flex;
    align-items: center;
  }

  .amount-box {

    .amount {
      color: #C00000;
      font-weight: bold;
    }
  }

  .btn-sellte {
    background-color: #C00000;
    height: 100%;
    color: white;
    line-height: 50px;
    padding: 0px 10px;
    min-width: 100px;
    text-align: center;
  }
</style>